﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RooHui.OA.Biz;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.Entity;

namespace RooHui.Admin.Web.Controllers
{
    public class ProcessController : Controller
    {
        //
        // GET: /Process/

        public ActionResult Index()
        {
            return View();
        }
        #region 流程定义
         
        public ActionResult GetProcessDefListByPage(int page, int pageSize, string key="")
        {
            int count = 0;
            var processDef = ProcessDefBiz.GetProcessDefListByPage(page, pageSize, key, ref count);
           
            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("Rows", processDef);
            dic.Add("Total", count);
            return Content(dic.ToJson());
        }

        [HttpPost]
        public ActionResult ProcessDefSave(string processDefName, int processDefType, int defID=0)
        {
            if (defID == 0)
            {

                if (ProcessDefBiz.ProcessDefExsit(processDefName))
                {
                    return Content("-1");
                }

                var processDef = ProcessDefBiz.AddProcessDef(processDefName,"", processDefType, System.Web.HttpContext.Current.User.Identity.Name);
                if (processDef == null)
                {
                    return Content("0");
                }
                else
                {
                    return Content("1");
                }
               
            }
            else
            {
                if (ProcessDefBiz.ProcessDefExsit(processDefName, defID))
                {
                    return Content("-1");
                }
                var processDef = ProcessDefBiz.EditProcessDef(defID, processDefName, "", processDefType); 

                if (processDef == null)
                {
                    return Content("0");
                }
                else
                {
                    return Content("1");
                }
            }
        }


        [HttpPost]
        public ActionResult ProcessDefDelete(string processDefIDs)
        {
            foreach (var id in processDefIDs.Split(','))
            {
                ProcessDefBiz.DeleteProcessDef(Convert.ToInt32(id));
            }

            return Content("1");
        }

        [HttpPost]
        public ActionResult ProcessDefChangeStatus(int processDefID, int status)
        {

            var user = ProcessDefBiz.ChangeStatus(processDefID, status);

            if (user == null)
            {
                return Content("0");
            }
            else
            {
                return Content("1");
            }
        }

        #endregion

        #region 流程节点
        public ActionResult ProcessProcList(int defID, int processDefType=1)
        {
            var processDef = new ProcessDef().Find(defID.ToString());
           // ViewBag.userList = UserBiz.GetActiveUserList("");
            ViewBag.processDef = processDef;
            return View();
        }

         
        public ActionResult GetProcessListByPageAndDefID(int page, int pageSize, int defID)
        {
            int count = 0;
            var process = ProcessBiz.GetProcessListByPageAndDefID(page, pageSize, defID, ref count);

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("Rows", process);
            dic.Add("Total", count);
            return Content(dic.ToJson());
        }

         [HttpPost]
         public ActionResult ProcessSave(int processDefID, string processName, int signType, string signUsers, int changeState, int processID=0)
         {
             if (processID == 0)
             {

                 if (ProcessBiz.ProcessExsit(processName))
                 {
                     return Content("-1");
                 }

                 var process = ProcessBiz.AddProcess(processDefID, processName, signType, signUsers, changeState);
                 if (process == null)
                 {
                     return Content("0");
                 }
                 else
                 {
                     return Content("1");
                 }

             }
             else
             {
                 if (ProcessBiz.ProcessExsit(processName, processID))
                 {
                     return Content("-1");
                 }
                 var processDef = ProcessBiz.EditProcess(processID,processDefID, processName, signType, signUsers, changeState);

                 if (processDef == null)
                 {
                     return Content("0");
                 }
                 else
                 {
                     return Content("1");
                 }
             }
         }


         [HttpPost]
         public ActionResult ProcessDelete(string processIDs)
         {
             foreach (var id in processIDs.Split(','))
             {
                 ProcessBiz.DeleteProcess(Convert.ToInt32(id));
             }

             return Content("1");
         }

         [HttpPost]
         public ActionResult ProcessChangeStatus(int processID, int status)
         {

             var user = ProcessBiz.ChangeStatus(processID, status);

             if (user == null)
             {
                 return Content("0");
             }
             else
             {
                 return Content("1");
             }
         }
        #endregion

        #region 流程步骤
          
         public ActionResult GetProcessListByDefID(int defID,int type=0,string key = "")
         {
             var processList = ProcessBiz.GetProcessListByDefID(defID, key);
             
             if (type == 0)
             {
                 processList.Insert(0, new Process() { ID = -1, ProcessName = "开始" });
             }
             else
             {
                 processList.Insert(0, new Process() { ID = -1, ProcessName = "结束" });
             }
             return Content(processList.ToJson());
         } 

        public ActionResult ProcessStepList(int defID)
        {
            var processDef = new ProcessDef().Find(defID.ToString());
            
            ViewBag.processDef = processDef;
            return View();
        }
        
        public ActionResult GetProcessStepListByPageAndDefID(int page, int pageSize, int defID)
        {
            int count = 0;
            var process = ProcessStepBiz.GetProcessStepListByPageAndDefID(page, pageSize, defID, ref count);

            Dictionary<string, object> dic = new Dictionary<string, object>();
            dic.Add("Rows", process);
            dic.Add("Total", count);
            return Content(dic.ToJson());
        }

        [HttpPost]
        public ActionResult ProcessStepSave(int processDefID, string stepName, int currentProcessID, int nextProcessID, int processStepID = 0)
        {
            if (processStepID == 0)
            {


                var processStep = ProcessStepBiz.AddProcessStep(processDefID, stepName, currentProcessID, nextProcessID);
                if (processStep == null)
                {
                    return Content("0");
                }
                else
                {
                    return Content("1");
                }

            }
            else
            {

                var processStep = ProcessStepBiz.EditProcessStep(processStepID, processDefID, stepName, currentProcessID, nextProcessID);

                if (processStep == null)
                {
                    return Content("0");
                }
                else
                {
                    return Content("1");
                }
            }
        }


        [HttpPost]
        public ActionResult ProcessStepDelete(string processStepIDs)
        {
            foreach (var id in processStepIDs.Split(','))
            {
                ProcessStepBiz.DeleteProcessStep(Convert.ToInt32(id));
            }

            return Content("1");
        }

        [HttpPost]
        public ActionResult ProcessStepChangeStatus(int processStepID, int status)
        {

            var user = ProcessStepBiz.ChangeStatus(processStepID, status);

            if (user == null)
            {
                return Content("0");
            }
            else
            {
                return Content("1");
            }
        }
        #endregion


    }
}
